package com.chengliang.zuoye.controller;

import com.chengliang.zuoye.bean.User;
import com.chengliang.zuoye.response.JsonResult;
import com.chengliang.zuoye.service.ClassService;
import com.chengliang.zuoye.service.MailService;
import com.chengliang.zuoye.service.RoleService;
import com.chengliang.zuoye.service.UserService;
import com.github.pagehelper.Page;
import com.wf.captcha.SpecCaptcha;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author jerry
 */
@Controller
@RequestMapping("/user")
public class UserController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private final UserService userService;

    private final MailService mailService;

    private final RoleService roleService;

    private final ClassService classService;

    @Autowired
    public UserController(MailService mailService, UserService userService, RoleService roleService, ClassService classService) {
        this.mailService = mailService;
        this.userService = userService;
        this.roleService = roleService;
        this.classService = classService;
    }

    /**
     * @param user
     * 修改用户信息多字段
     */
    @PostMapping("/update")
    @ResponseBody
    public Object update(User user){
        return JsonResult.is(userService.updateUserManyById(user),"修改");
    }
    /**
     * 修改页面
     * @param id
     * @param model
     * @return
     */
    @GetMapping("update/{id}")
    public String updatePage(@PathVariable Integer id,Model model){
        model.addAttribute("roleList",roleService.roleList());
        model.addAttribute("classList",classService.classList());
        model.addAttribute("user",userService.getUserById(id));
        return "user/update";
    }

    /**
     * @param user
     * @return
     * 添加
     */
    @PostMapping("/add")
    @ResponseBody
    public Object add(User user){
        return userService.saveUser(user)?JsonResult.ok("添加成功,默认密码123456"):JsonResult.error("添加失败");
    }
    /**
     * 添加页面
     */
    @GetMapping("/add")
    public String addPage(Model model){
        model.addAttribute("roleList",roleService.roleList());
        model.addAttribute("classList",classService.classList());
        return "user/add";
    }

    /**
     * 用户列表数据
     */
    @GetMapping("/data/json")
    @ResponseBody
    public Object list(User user,@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit) {
        Page<User> userList = userService.getUserList(user, page, limit);
        return JsonResult.ok(userList.getResult()).append("count",userList.getTotal());
    }

    /**
     * 用户列表页面
     */
    @GetMapping("/list")
    public String listPage(Model model) {
        model.addAttribute("roleList",roleService.roleList());
        model.addAttribute("classList",classService.classList());
        return "user/list";
    }

    /**
     * 修改个人信息
     * @param user
     * @return
     */
    @PostMapping("/update/my")
    @ResponseBody
    public Object updateMyUser(User user) {
        //使用当前用户ID
        User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        user.setId(principal.getId());
        return JsonResult.is(userService.updateUserById(user),"资料修改");
    }

    /**
     * @return 我的资料
     */
    @RequestMapping("/info")
    public String info(Model model) {
        model.addAttribute("classList", classService.classList());
        model.addAttribute("roles", roleService.roleList());
        return "user/info";
    }

    /**
     * 登录页面
     *
     * @return
     */
    @GetMapping("/login")
    public String getLogin() {
        return "user/login";
    }

    /**
     * @param model
     * @return 用户被挤下线
     */
    @GetMapping("/offline")
    public String offline(Model model) {
        model.addAttribute("msg", "该账户已在其他机器登录,如不是本人操作请及时修改密码");
        return "user/login";
    }

    /**
     * 注册页面
     *
     * @return
     */
    @GetMapping("/reg")
    public String regPage() {
        return "user/reg";
    }

    @PostMapping("/reg")
    @ResponseBody
    public Object reg(User user) {
        return JsonResult.is(userService.register(user),"注册");
    }

    /**
     * @param email
     * @param request
     * @return 邮箱验证码
     */
    @PostMapping("/veremail")
    @ResponseBody
    public Object veremail(String email, HttpServletRequest request) {
        SpecCaptcha specCaptcha = new SpecCaptcha();
        String code = specCaptcha.text();
        //发送验证邮件
        String content = "注册验证码: " + code;
        boolean b = mailService.sendHtmlMail(email, "RIMI作业管理平台注册", content);
        if (!b) {
            return JsonResult.error("服务器邮件发送失败,请稍后重试");
        }
        // 验证码存入session
        request.getSession().setAttribute("emailCaptcha", code.toLowerCase());
        return JsonResult.ok("验证码已经发送到邮箱中,请查看邮件");
    }


}
